var Logic	= {
	board	: null,
	score	: 0,
	combo	: 1,
	BLOCK	: false,

	actions : null,

	selection	: null,
	
	start	: function(){
		var nx	= 6;
		var ny	= 10;

		UI.start(nx, ny);

		Logic.score	= 0;
		Logic.board	= new Array();
		Logic.actions	= new Array();

		for(var x = 0; x < nx; x++){
			Logic.board.push(new Array());
		}
		
		for(var x = 0; x < nx; x++){
			for(var y = 0; y < ny; y++){
				Logic.board[x].push(0);	
			}
		}
		
		for(var n = 0; n < 4; n++){
			Logic.newLine();
		}
	
		Logic.updateBoard();
		Logic.updateScore();
		Logic.updateActions();
		
		//window.setInterval(Logic.newLine, 5000);
	},

	

	newLine		: function(){
		var line = new Array();
		var nx = Logic.board.length;
		var ny = Logic.board[0].length;
		
		for(var n = 0; n < nx; n++){
			var done = false;
			while(!done){
				r = Logic.getRandom(nx)+1;
				var l1 = 0;
				var l2 = 0;
				var u1 = Logic.board[n][ny-1];
				var u2 = Logic.board[n][ny-2];

				if(n > 1){
					l2 = Logic.board[n-2][ny-1];
					l1 = Logic.board[n-1][ny-1];
				}
				
				if((l2 == r && l1 == r) || (u2 == r && u1 == r)){
					//console.log("V"+ n + ": "+ u2 + "-"+ u1 + "-"+ r);
					//console.log("H"+ n + ": "+ l2 + "-"+ l1 + "-"+ r);
				}else{
					done = true;
				}
			}
			Logic.board[n].shift();
			Logic.board[n].push(r);
		}
	},

	onMouseDown	: function(tile){
		Logic.selectTile(tile);
		Logic.updateActions();
	},
	
	onMouseMove	: function(tile){
		if(Logic.switchTile(tile)){
			Logic.updateActions();
		}
		
	},

	onMouseUp	: function(){
		Logic.deselectTile();
		Logic.updateActions();
	},

	selectTile	: function(tile){
		var newTile = Logic.getTile(tile.x, tile.y);
		if(newTile.value != 0){
			Logic.selection = newTile;
			Logic.updateSelection();
		}
	},

	deselectTile	: function(){
		if(Logic.selection != null){
			var tile = Logic.getTile(Logic.selection.x, Logic.selection.y);
			Logic.updateDeselection();
			Logic.selection = null;
		}
	},
	
	removeTile		: function(pos){
		Logic.board[pos.x][pos.y] = 0;
	},
	
	getTile		: function(x, y){
		var tile = Tools.newTile(x, y, Logic.board[x][y]);
		return tile;
	},

	switchTile	: function(tile){
		if(Logic.selection != null){
			if(Logic.selection.x != tile.x && Logic.selection.y == tile.y){
				Logic.combo = 1;
				var oldTile = Logic.getTile(Logic.selection.x, Logic.selection.y);
				var newTile = Logic.getTile(tile.x, tile.y);

				Logic.board[oldTile.x][oldTile.y] = newTile.value;
				Logic.board[newTile.x][newTile.y] = oldTile.value;

				var some = new Array();
				for(x in this.board){
					for(y in this.board[0]){
						some.push(Tools.newTile(x, y, this.board[x][y]));
					}
				}
				UI.updateSome(some);
		
				var newValue = newTile.value;
				newTile.value = oldTile.value;
				oldTile.value = newValue;
				Logic.selectTile(newTile);
				
				Logic.checkSome(new Array(newTile, oldTile));
				
				Logic.fallAndCheck();
				return true;
			}
		}
		return false;
	},

	fallAndCheck	: function(){
		var fallen = true;
		while(fallen){
			var falling = Logic.fallTiles();
			if(falling.length > 0){
				Logic.checkSome(falling);
			}else{
				fallen = false;
			}
		}
		Logic.updateActions();
	},

	fallTiles		: function(){
		var falling = new Array();
		var all = new Array();
		for(var x = 0; x < Logic.board.length; x++){
			var f = 0;
			for(var y = Logic.board[x].length; y >= 0; y--){
				if(Logic.board[x][y] == 0){
					f++;
				}else{
					var value = Logic.board[x][y];
					all.push(Tools.newTile(x, y, value, f));
					if(f > 0){
						var yf = y + f;
						Logic.board[x][y] = 0;
						Logic.board[x][yf] = value;
						falling.push(Tools.newTile(x, yf, value, f));
					}
				}
			}
		}
		
		if(falling.length > 0){
			Logic.updateFalling(all);
			Logic.selection = null;
		}

		return falling;
	},

	updateActions	: function(){
		UI.doStuff(Logic.actions);
	},

	checkAll	: function(){
		for(x in Logic.board){
			for(y in Logic.board[x]){
				if(Logic.board[x][y] != 0){
					var rem = Logic.checkTile(x, y);
					if(rem.length > 0){
						Logic.removeTiles(rem);
						Logic.addScore(rem.length);
						Logic.selection = null;
					}
				}
			}
		}
	},

	checkSome	: function(some){
		for(s in some){
			var tile = some[s];
			var rem = Logic.checkTile(tile.x, tile.y);
			if(rem.length > 0){
				Logic.removeTiles(rem);
				Logic.addScore(rem.length);
				Logic.selection = null;
			}
		}
	},

	checkTile		: function(px, py){
		var tile = Logic.getTile(px, py);

		var x = y = v = 0;

		x = Math.abs(tile.x);
		y = Math.abs(tile.y);
		v = tile.value;
		
		var tilesV = new Array();
		var tilesH = new Array();
		var tilesHV = new Array();
		
		if(v > 0){
			for(var yy = y; yy >= 0 && Logic.board[x][yy] == v; yy--){
				tilesV.push(Logic.getTile(x, yy));
			}
			for(var yy = y+1; yy < Logic.board[0].length && Logic.board[x][yy] == v; yy++){
				tilesV.push(Logic.getTile(x, yy));
			}

			for(var xx = x; xx >= 0 && Logic.board[xx][y] == v; xx--){
				tilesH.push(Logic.getTile(xx, y));
			}
			for(var xx = x+1; xx < Logic.board.length && Logic.board[xx][y] == v; xx++){
				tilesH.push(Logic.getTile(xx, y));
			}
			
			if(tilesV.length > 2){
				for(t in tilesV){
					tilesHV.push(tilesV[t]);
				}
			}
			
			if(tilesH.length > 2){
				for(t in tilesH){
					tilesHV.push(tilesH[t]);
				}
			}
		}

		return tilesHV;
	},

	removeTiles		: function(tiles){
		var moreTiles = new Array();
		for(var x = 0; x < Logic.board.length; x++){
			for(var y = Logic.board[x].length; y >= 0; y--){
				var f = 0;
				for(var t = 0; t < tiles.length && f == 0; t++){
					if(tiles[t].x == x && tiles[t].y == y){
						f = 1;
					}
				}
				moreTiles.push(Tools.newTile(x, y, Logic.board[x][y], f));
			}
		}
		
		for(t in tiles){
			Logic.removeTile(tiles[t]);
		}
		Logic.combo++;
		Logic.updateRemove(moreTiles);
	},

	getRandom		: function(n){
		return Math.floor(Math.random()*n);
	},

	addScore	: function(n){
		if(n < 3){
			return false;
		}
		var add		= 7;
		var bonus	= Math.pow(3, n-3);
		var points	= add * bonus * Logic.combo;
		var result	= Logic.score + points;
		Logic.score = result;
		Logic.updateScore();
	},
	
	updateBoard	: function(){
		Logic.actions.push(Tools.actionBoard(Logic.board));
	},

	updateScore	: function(){
		Logic.actions.push(Tools.actionScore(Logic.score));
	},
	
	updateSelection	: function(){
		Logic.actions.push(Tools.actionSelection(Logic.selection));
	},
	
	updateDeselection	: function(){
		Logic.actions.push(Tools.actionDeselection(Logic.selection));
	},
	
	updateSwitch	: function(tileA, tileB){

		var tiles = new Array(2);
		tiles[0] = Tools.newTile(tileA.x, tileA.y, tileA.value);
		tiles[1] = Tools.newTile(tileB.x, tileB.y, tileB.value);
		Logic.actions.push(Tools.actionSwitch(tiles));

		//Logic.updateBoard();
	},

	updateRemove	: function(tiles){
		Logic.actions.push(Tools.actionRemove(tiles));
	},
	
	updateFalling	: function(falling){
		Logic.actions.push(Tools.actionFalling(falling));
	}
}
